package com.lovo.movie.controller;

//import com.lovo.vo.MovieVo;

import com.lovo.movie.config.RedisCacheUtil;
import com.lovo.movie.dao.IOrderDao;
import com.lovo.movie.entity.MovieEntity;
import com.lovo.movie.entity.OrderEntity;
import com.lovo.movie.service.ITicketRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
public class MovieController {
    @Autowired
    RedisCacheUtil redisCacheUtil;

    @Autowired
    private IOrderDao orderDao;
    @Autowired
    ITicketRemote ticketRemote;
 @GetMapping("orderList")
  public List<OrderEntity> orderList(){
   return (List<OrderEntity>) orderDao.findAll();
  }
  @GetMapping("changeOrder/{tag}/{orderCode}")
  public  boolean changeOrder(@PathVariable("tag")int tag,@PathVariable("orderCode") String orderCode){
     if(tag==1) {
         ticketRemote.changeOrder(orderCode);
         return true;
     }
    return false;
  }

  @GetMapping("/addShops/{goodsName}/{number}")
    public void addShops(@PathVariable("goodsName") String goodsName,
                         @PathVariable("number") int number){

      MovieEntity goodsDto=new MovieEntity();

      goodsDto.setMovieName(goodsName);
      goodsDto.setMovieNum(number);

      //用户ID

      redisCacheUtil.hset("waww",goodsDto.getMovieName()+"_name",goodsDto.getMovieName());
      redisCacheUtil.hset("waww",goodsDto.getMovieName()+"_num",goodsDto.getMovieNum());

      //把filed放入到set
      redisCacheUtil.sSet("movie",goodsDto.getMovieName());
  }

    @GetMapping("/getShops")
    public List<MovieEntity> getShops(){
        List<MovieEntity> list = new ArrayList<>();
        Set<Object> sets = redisCacheUtil.sGet("movie");
        for(Object obj:sets){
            String goodsName= (String) redisCacheUtil.hget("waww",obj.toString()+"_name");
            int num= Integer.parseInt(String.valueOf(redisCacheUtil.hget("waww",obj.toString()+"_num")));
            System.out.println(goodsName+"_"+num);
            MovieEntity movieEntity = new MovieEntity();
            movieEntity.setMovieName(goodsName);
            movieEntity.setMovieNum(num);
            list.add((movieEntity));
        }
        return list;
    }

    @GetMapping("/updateShop/{movieName}/{number}")
    public void updateShop(@PathVariable("number") String number,@PathVariable("movieName") String movieName){
        redisCacheUtil.hset("waww",movieName+"_num",number);
        }

    @GetMapping("/deleteShop/{movieName}")
    public void delete(@PathVariable("movieName") String movieName){
        Set<Object> sets = redisCacheUtil.sGet("movie");
        redisCacheUtil.del("movie");
        for (Object obj:sets) {
            if(obj.toString().equals(movieName)){
                sets.remove(obj);
            }
        }

        for (Object obj:sets) {
            redisCacheUtil.sSet("movie",obj.toString());
        }
    }

}
